<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">
               {{$store.state.city}}
            </div>
          </div>

        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper"
               v-for="item in hot"
               v-bind:key="item.id"

          >
            <div class="button" @click="getCities(item.name)">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area"
           v-for="(city,key) in cities"
           :ref="key"
      >
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom" v-for="item in city" :key="item.id" @click="getCities(item.name)">{{item.name}}</div>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
  import Bscroll from 'better-scroll'
  export default {
    props:{
      cities:Object,
      hot:Array,
      letter:String
    },
    data() {
      return {}
    },
    methods:{
      getCities(city){
         this.$store.dispatch('changeCities',city)
        this.$router.push('/prefile')
      }
    },
    mounted(){
       this.scroll = new Bscroll(this.$refs.wrapper, {click: true})
    },
    watch: {    // 监听letter 数据变化
     letter(){
        if(this.letter){
          const element = this.$refs[this.letter][0]; // 获取字母对应的区域 获取的是一个数组
          this.scroll.scrollToElement(element)
        }
      }
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../common/stylus/minxies.styl"
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    overflow: hidden
    position: absolute
    top: 90px
    left: 0
    right: 0
    bottom: 0
    .title
      line-height: 28px
      background: #eee
      padding-left: 12px
      color: #666
      font-size: 14px
    .button-list
      overflow: hidden
      padding:2px
      .button-wrapper
        float: left
        width: 31.33%
        box-sizing border-box
        padding-left:8px
        .button
          margin: 8px
          padding: 3px 0
          text-align: center
          border-radius: 4px
          border:1px solid #ccc
    .item-list
      .item
        line-height: 40px
        padding-left: 10px
      .border-bottom
        border-bottom 1px solid #ccc
</style>
